import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	login,
	logout,
	getInfo
} from '@/api/login'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'

const baseUrl = config.baseUrl
const user = {
	//状态存储
	state: {
		token: getToken(),
		name: storage.get(constant.name),
		avatar: storage.get(constant.avatar),
		roles: storage.get(constant.roles),
		permissions: storage.get(constant.permissions),
		balance: 0,
		integral: 0,
		pid: 0,
		openid: 0,
		shareCount: 0,
	},
	//状态变化，正常都会被actions调用
	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_NAME: (state, name) => {
			state.name = name
			storage.set(constant.name, name)
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles
			storage.set(constant.roles, roles)
		},
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions
			storage.set(constant.permissions, permissions)
		},
		SET_BALANCE: (state, balance) => {
			state.balance = balance;
		},
		SET_INTEGRAL: (state, integral) => {
			state.integral = integral;
		},
		SET_PID: (state, pid) => {
			state.pid = pid;
		},
		SET_OPENID: (state, openid) => {
			state.openid = openid;
		},
		SET_SHARECOUNT: (state, shareCount) => {
			state.shareCount = shareCount;
		},
	},
	//行为变化-》状态变化
	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			return new Promise((resolve, reject) => {
				login(userInfo).then(res => {
					res = res.data
					setToken(res.access_token)
					commit('SET_TOKEN', res.access_token)
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo().then(res => {
					if (res.code === 200) {
						const user = res.data;
						const roles = res.data.roles || [];
						const permissions = res.data.permissions || [];
						const avatar = user.avatar ? user.avatar : require(
							"@/static/images/profile.jpg");
						const nickname = user.nickname || '';
						const balance = user.balance || 0;
						const integral = user.integral || 0;
						const pid = user.pid || 0;
						const openid = user.openid || 0;
						const shareCount = user.shareCount || 0;

						commit('SET_ROLES', roles);
						commit('SET_PERMISSIONS', permissions);
						commit('SET_NAME', nickname);
						commit('SET_AVATAR', avatar);
						commit('SET_BALANCE', balance);
						commit('SET_INTEGRAL', integral);
						commit('SET_PID', pid);
						commit('SET_OPENID', openid);
						commit('SET_SHARECOUNT', shareCount);
						resolve(res);
					} else {
						reject(new Error(res.msg || '获取用户信息失败'));
					}
				}).catch(error => {
					reject(error);
				});

			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				logout(state.token).then(() => {
					commit('SET_TOKEN', '')
					commit('SET_ROLES', [])
					commit('SET_PERMISSIONS', [])
					commit('SET_NAME', '')
					commit('SET_AVATAR',
						'https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp'
					)
					commit('SET_BALANCE', 0)
					commit('SET_INTEGRAL', 0)
					removeToken()
					storage.clean()
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
}

export default user